//今儿又是活力满满的一天呢~
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
inline int GetMonthDay(int year, int month)
{
	int array[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int day = array[month];
	if ((month == 2) && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
	{
		day++;
	}
	return day;
}
class Date
{
public:
	Date(int year = 0, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date& Date::operator+=(int day)
	{
		_day += day;
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month > 12)
			{
				_year++;
				_month = 1;
			}
		}
		return *this;
	}

	void set_year(int year)
	{
		_year = year;
	}
	void set_month(int month)
	{
		_month = month;
	}
	void set_day(int day)
	{
		_day = day;
	}
	void Print()
	{
		cout << _year <<" " << _month <<" " <<  _day <<" " << endl;
	}
private:
	int _year;
	int _month;
	int _day;

};

int main(void)
{
	int m;
	cin >> m;
	while (m--)
	{
		int year, month, day, n;
		cin >> year >> month >> day >> n;
		Date d1(year, month, day);
		d1 += n;
		d1.Print();
	}

	return 0;
}